 
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/base_entity.dart';
import 'package:flutter_ncsjqz_getx/page/square/details/guang_chang_details_view.dart';
import 'package:flutter_ncsjqz_getx/widget/app_bar_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/base_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/gradient_button.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:get/get.dart';

import 'message_info_logic.dart';
import 'sys_message_view.dart';

/// <summary>
/// todo: 消息
/// author：zwb
/// dateTime： 2021/11/11 18:20
/// filePath： lib/page/my/message/message_info_view.dart
/// desc:
/// <summary>
///
class MessageInfoPage extends StatefulWidget {
  String title;
  MessageInfoPage({this.title:''});
  @override
  _MessageInfoPageState createState() => _MessageInfoPageState();
}

class _MessageInfoPageState extends State<MessageInfoPage> {
  final logic = Get.put(MessageInfoLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData(load: false);
  }

  getData({bool load: false}){
    if(widget.title =="系统公告"){
      logic.getSysNotice();
    }
    //1 平台 3评论
    if(widget.title =="平台消息"){
      logic.getPtAndPlMessage(1,load: load);
    }
    if(widget.title =="评论"){
      logic.getPtAndPlMessage(3,load: load);
    }
  }
  @override
  Widget build(BuildContext context) {
    return GetBuilder<MessageInfoLogic>(
      builder: (controller) {
        return Scaffold(
          appBar: appBarWidget(title: "${widget.title}"),
          backgroundColor: Colors.white,
          body: baseScaffoldWidget(
            onRefresh: () async{
              getData(load: false);
            },
            onLoad: ()async{
              getData(load: true);
            },
            child: ListView(
              children: List.generate(widget.title =="系统公告" ? logic.sysNoticeData!.length : logic.msgList!.length , (index){
                return InkWell(
                  onTap: () async{
                    await DioService<BaseEntity>().changeMessageStatus({"msgId":widget.title =="系统公告" ?logic.sysNoticeData![index].id : logic.msgList![index].id});
                    if(widget.title =="系统公告" ){
                      Get.to(()=>SysMessageView(sysId: logic.sysNoticeData![index].id,))!.then((value) => logic.getSysNotice());
                    }
                    // 关注提醒
                    if((widget.title =="评论" || widget.title =="平台消息" ) && logic.msgList![index].msgTypeId==null){
                      Get.to(()=>SysMessageView(content: logic.msgList![index],))!.then((value) => logic.getPtAndPlMessage(widget.title =="评论" ? 3 :1));
                    }
                    // 评论、回复提醒
                    if(widget.title == "评论" && logic.msgList![index].msgTypeId!=null && logic.msgList![index].type != 302){
                      Get.to(()=>GuangChangDetailsPage(),arguments:logic.msgList![index].msgTypeId )!.then((value) => logic.getPtAndPlMessage(3));
                    }
                  },
                  child: Container(
                    margin: EdgeInsets.symmetric(horizontal: 15),
                    padding: EdgeInsets.symmetric(vertical: 15),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                InkWidget.title("${widget.title =="系统公告" ? logic.sysNoticeData![index].msgTitle : logic.msgList![index].msgTitle}"),
                                Builder(builder: (ctx){
                                  Color color = Colors.transparent;
                                  if(widget.title =="系统公告"){
                                    color = logic.sysNoticeData![index].msgStatus==0 ? Colors.red:Colors.transparent;
                                  }else{
                                    color = logic.msgList![index].msgStatus == 1 ? Colors.transparent:Colors.red;
                                  }
                                  return ClipOval(
                                    child: Container(
                                      width: 8,
                                      height: 8,
                                      color: color,
                                    ),
                                  );
                                })
                              ],
                            ),
                            Expanded(child: InkWidget.text(" ${widget.title =="系统公告" ? logic.sysNoticeData![index].msgContent : logic.msgList![index].msgContent}",maxLines: 1,fonSize: 14,color: Colors.grey),),
                            widget.title =="评论" &&  logic.msgList![index].type == 302 ?SizedBox(
                              width: 75,
                              height: 30,
                              child: gradientButton(logic.msgList![index].isFollow==true ? "已关注" : "关注",bgColor: logic.msgList![index].isFollow==true ?Colors.grey:null,onTap: logic.msgList![index].isFollow==true ?null:(){
                                print("关注");
                                logic.setFollow(logic.msgList![index].msgTypeId);
                              }),
                            ):SizedBox(),
                          ],
                        ),
                        InkWidget.text("${widget.title =="系统公告" ? logic.sysNoticeData![index].createTime : logic.msgList![index].createTime}",fonSize: 14,color: Colors.grey,maxLines: 999999),
                      ],
                    ),
                    decoration: BoxDecoration(
                      border: Border(bottom: BorderSide(color: Colors.grey.shade200,),),
                    ),
                  ),
                );
              }),
            )
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<MessageInfoLogic>();
    super.dispose();
  }
}